$(function () {
    useLayUI();
})

//实例化vue:
var base = new Vue({
    el: "#setting_base",
    data() {
        return {
            services: services,
            userInfo:"",
            id:"",
            img:"",
            name:"",
            gender:"",
            birthday:"",
            firstWorkTime:"",
            email:"",
            wechat:"",
            webSite:"",
            tel:"",
            sex:"",
            options:[{text:"男", value:1},{text:"女", value:2}]
        }
    },
    created () {
        this.userInfo = LoginStatus();
        if(this.userInfo==false)
            return false;
        //信息初始化
        this.name = this.userInfo.name;
        this.img = this.userInfo.img
        this.birthday = this.userInfo.birthday;
        this.firstWorkTime = this.userInfo.firstWorkTime
        this.email = this.userInfo.email
        this.wechat = this.userInfo.wechat
        this.webSite = this.userInfo.webSite
        this.tel = this.userInfo.tel
        //性别
        this.gender = this.userInfo.gender;
    },
    methods:{
        submit: function () {
            this.birthday = $('input[name="birthday"]').val();
            this.firstWorkTime = $('input[name="firstWorkTime"]').val();
            this.img = $('input[name="img"]').val();
            var that = this;
            //数据校验
            if(that.name==""||that.name==null){
                layerMsg("名字不为空")
                return false;
            }
            //ajax请求
            $.ajax({
                type:'post',
                async:false,
                url:that.services+"/user/info",
                contentType:"application/json;charset=utf-8",
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                data:JSON.stringify({
                    'name':that.name,
                    'img':that.img,
                    'gender':that.gender,
                    'birthday':that.birthday,
                    'firstWorkTime':that.firstWorkTime,
                    'email':that.email,
                    'wechat':that.wechat,
                    'webSite':that.webSite,
                }),
                success: function(res) {
                    var check = ApiLoginStatus(res)
                    if(check==false)
                        return false;
                    layerMsg(res.message);
                    if(res.success){
                        data = res.data
                        var userInfodata = JSON.stringify(res.data);
                        $.cookie('userInfo',userInfodata)
                        setTimeout(function() {
                            window.location.href="my-resume.html" ;
                        },500);
                    }
                }
            })
        }
    }
});
//监听select
layui.use(['form','upload'] ,function () {
    form = layui.form;
    form.on('select', function (data) {
        base.gender = data.value;
    });
    //普通图片上传
    upload = layui.upload;
    upload.render({
        elem: '#imageUrlUpload'
        , url: services+"/user/upload/img"
        , before: function (obj) {
            //预读本地文件示例，不支持ie8

        }
        , done: function (res) {
            if(res.code==0){
                var fullPath = services + res.data.src;
                $('#imageUrlUpload').attr('src',fullPath);
                $('#img').val(res.data.src) ;
                console.log(res)
                layer.msg(res.msg);
            }
        }
        , error: function () {
            layer.msg("上传失败");
        }
    });
});